package com.yycx.module.bus.provider.handler.haiying.salemanbalance;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.module.admin.provider.mapper.BaseUserDeptMapper;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.entity.BusSalemanBalance;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.entity.hy.HyOrderDetail;
import org.flowable.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 资保金抵扣记录
 *
 * @author zyf
 */
@Component
public class SaleBalanceTab2PageListHandler implements PageInterceptor<BusSalemanBalance> {

    @Resource
    private BaseUserDeptMapper userDeptMapper;

    @Autowired
    private TaskService taskService;

    @Override
    public void prepare(CriteriaQuery<BusSalemanBalance> cq, PageParams pageParams, EntityMap params) {
        OpenUser openUser = OpenHelper.getUser();
        Long companyId = params.getLong("companyId", openUser.getCompanyId());
        cq.select(BusSalemanBalance.class, "*");
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        cq.addSelect(BusSaleman.class, "saleManName");
        cq.addSelect(HyOrder.class, "orderNo", "sendDate");
        cq.addSelect(HyOrderDetail.class, "drugsName", "drugsSpec", "saleNum", "salePrice");
        cq.eq(true, "optType", CommonConstants.INT_2);
        cq.eq(true, "bsb.companyId", companyId);
        cq.isNotNull("bhd.saleManBalanceId");
        cq.ge(FlymeUtils.isNotEmpty(beginDate), "payDate", beginDate);
        cq.le(FlymeUtils.isNotEmpty(endDate), "payDate", endDate);
        cq.createJoin(BusSaleman.class);
        cq.createJoin(HyOrder.class);
        cq.createJoin(HyOrderDetail.class);
        cq.orderByDesc("bsb.createTime");
    }

}
